package com.cgbl.cg.ui.mine

import android.graphics.Color
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.WorksAdapter
import com.cgbl.cg.databinding.AppActivityWorksDataBinding
import com.cgbl.cg.vm.mine.PersonalWorksDataViewModel
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.github.mikephil.charting.data.LineData

import com.github.mikephil.charting.utils.ColorTemplate

import com.github.mikephil.charting.components.YAxis.AxisDependency
import com.github.mikephil.charting.data.Entry

import com.github.mikephil.charting.data.LineDataSet

import com.github.mikephil.charting.components.Legend

import com.github.mikephil.charting.components.Legend.LegendForm


@Route(path = AppConstants.Router.Mine.A_MINE_PERSONAL_WORKS_DATA)
class PersonalWorksDataActivity :
    GLBaseActivity<AppActivityWorksDataBinding, PersonalWorksDataViewModel>() {


    override fun initComponents() {
        initTiterBar()
        initView()
    }

    private fun initTiterBar() {
        val titleView = findViewById<TextView>(R.id.tv_title)
        val backView = findViewById<ImageView>(R.id.iv_back)
        titleView.text = "作品数据"
        backView.setOnClickListener { finish() }
    }

    private fun initView() {
        var chart = binding.chart
        // no description text
        chart.getDescription().setEnabled(false);

        // enable touch gestures
        chart.setTouchEnabled(true);

        chart.setDragDecelerationFrictionCoef(0.9f);

        // enable scaling and dragging
        chart.setDragEnabled(true);
        chart.setScaleEnabled(true);
        chart.setDrawGridBackground(false);
        chart.setHighlightPerDragEnabled(true);

        // if disabled, scaling can be done on x- and y-axis separately
        chart.setPinchZoom(true);

        // set an alternative background color
        chart.setBackgroundColor(Color.WHITE);
        chart.animateX(1500);
        val l = chart.legend

        // modify the legend ...

        // modify the legend ...
        l.form = LegendForm.LINE
        l.textSize = 11f
        l.textColor = Color.WHITE
        l.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM
        l.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT
        l.orientation = Legend.LegendOrientation.HORIZONTAL
        l.setDrawInside(false)
//        l.setYOffset(11f);

        //        l.setYOffset(11f);
        val xAxis = chart.xAxis
        xAxis.textSize = 11f
        xAxis.textColor = Color.WHITE
        xAxis.setDrawGridLines(false)
        xAxis.setDrawAxisLine(false)

        val leftAxis = chart.axisLeft
        leftAxis.textColor = ColorTemplate.getHoloBlue()
        leftAxis.axisMaximum = 200f
        leftAxis.axisMinimum = 0f
        leftAxis.setDrawGridLines(false)
        leftAxis.setDrawZeroLine(true)
        leftAxis.isGranularityEnabled = true

        val rightAxis = chart.axisRight
        rightAxis.textColor = Color.RED
        rightAxis.axisMaximum = 900f
        rightAxis.axisMinimum = -200f
        rightAxis.setDrawGridLines(false)
        rightAxis.setDrawZeroLine(true)
        rightAxis.isGranularityEnabled = false
        chart.axisRight.isEnabled = false


        setData(10, 100f)
    }

    private fun setData(count: Int, range: Float) {
        var chart = binding.chart
        val values1 = mutableListOf<Entry>()
        for (i in 0 until count) {
            val randomValue = (Math.random() * (range / 2f)).toFloat() + 50
            values1.add(Entry(i.toFloat(), randomValue))
        }

        val values2 = mutableListOf<Entry>()
        for (i in 0 until count) {
            val randomValue = (Math.random() * (range / 2f)).toFloat() + 450
            values2.add(Entry(i.toFloat(), randomValue))
        }

        val values3 = mutableListOf<Entry>()
        for (i in 0 until count) {
            val randomValue = (Math.random() * (range / 2f)).toFloat() + 500
            values3.add(Entry(i.toFloat(), randomValue))
        }

        var set1: LineDataSet
        var set2: LineDataSet
        var set3: LineDataSet

        if (chart.data != null && chart.data.dataSetCount > 0) {
            set1 = chart.data.getDataSetByIndex(0) as LineDataSet
            set2 = chart.data.getDataSetByIndex(1) as LineDataSet
            set3 = chart.data.getDataSetByIndex(2) as LineDataSet
            set1.values = values1
            set2.values = values2
            set3.values = values3
            chart.data.notifyDataChanged()
            chart.notifyDataSetChanged()
        } else {
            // create a dataset and give it a type
            set1 = LineDataSet(values1, "DataSet 1");

            set1.setAxisDependency(AxisDependency.LEFT);
            set1.setColor(ColorTemplate.getHoloBlue());
            set1.setLineWidth(2f);
            set1.setFillAlpha(65);
            set1.setFillColor(ColorTemplate.getHoloBlue());
            set1.setHighLightColor(Color.rgb(244, 117, 117));
            set1.setDrawCircleHole(false);
            set1.setDrawCircles(false)
            set1.mode = LineDataSet.Mode.CUBIC_BEZIER;
            //set1.setFillFormatter(new MyFillFormatter(0f));
            //set1.setDrawHorizontalHighlightIndicator(false);
            //set1.setVisible(false);
            //set1.setCircleHoleColor(Color.WHITE);

            // create a dataset and give it a type
            set2 = LineDataSet(values2, "DataSet 2");
            set2.setAxisDependency(AxisDependency.RIGHT);
            set2.setColor(Color.RED);
            set2.setCircleColor(Color.WHITE);
            set2.setLineWidth(2f);
            set2.setCircleRadius(3f);
            set2.mode = LineDataSet.Mode.CUBIC_BEZIER;
            set2.setFillAlpha(65);
            set2.setFillColor(Color.RED);
            set2.setDrawCircleHole(false);
            set2.setDrawCircles(false)
            set2.setHighLightColor(Color.rgb(244, 117, 117));
            //set2.setFillFormatter(new MyFillFormatter(900f));

            set3 = LineDataSet(values3, "DataSet 3");
            set3.setAxisDependency(AxisDependency.RIGHT);
            set3.setColor(Color.YELLOW);
            set3.setCircleColor(Color.WHITE);
            set3.setLineWidth(2f);
            set3.mode = LineDataSet.Mode.CUBIC_BEZIER;
            set3.setCircleRadius(3f);
            set3.setFillAlpha(65);
            set3.setFillColor(ColorTemplate.colorWithAlpha(Color.YELLOW, 200));
            set3.setDrawCircleHole(false);
            set3.setDrawCircles(false)
            set3.setHighLightColor(Color.rgb(244, 117, 117));

            // create a data object with the data sets
            val data = LineData(set1, set2, set3);
            data.setValueTextSize(0f)
            // set data
            chart.setData(data);
        }

    }

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_works_data

    override fun initVariableId(): Int = BR.viewModel
}